package com.fanfan.SmartPensionSystembackend.mapper;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.fanfan.SmartPensionSystembackend.entity.Bed;
import com.fanfan.SmartPensionSystembackend.entity.BedInfo;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface BedMapper extends BaseMapper<Bed> {
    // 查询最大ID
    @Select("SELECT MAX(CAST(bedId AS UNSIGNED)) FROM bed")
    Integer selectMaxId();

    // 插入数据
    @Insert({
            "INSERT INTO bed (bedId, bedNo, buildingNo, roomNo, floorNo, price, bedPlace)",
            "VALUES (#{bedId}, #{bedNo}, #{buildingNo}, #{roomNo}, #{floorNo}, #{price}, #{bedPlace})"
    })
    void insertBed(Bed bed);

    // 查询所有床位
    @Select("SELECT * FROM bed")
    List<Bed> getAllBedList();

    // 根据 bedId 查询床位信息
    default BedInfo getBedInfoByBedId(String bedId) {
        Bed bed = selectOne(new LambdaQueryWrapper<Bed>()
                .select(Bed::getBedNo, Bed::getBuildingNo, Bed::getRoomNo, Bed::getFloorNo)
                .eq(Bed::getBedId, bedId)
        );
        if (bed == null) return null;
        BedInfo bedInfo = new BedInfo();
        bedInfo.setBedNo(bed.getBedNo());
        bedInfo.setBuildingNo(bed.getBuildingNo());
        bedInfo.setRoomNo(bed.getRoomNo());
        bedInfo.setFloorNo(bed.getFloorNo());
        return bedInfo;
    }

    // 根据 buildingNo, floorNo, roomNo 筛选床位
    @Select("SELECT * FROM bed WHERE buildingNo = #{buildingNo} AND floorNo = #{floorNo} AND roomNo = #{roomNo}")
    List<Bed> screenBeds(@Param("buildingNo") String buildingNo, @Param("floorNo") String floorNo, @Param("roomNo") String roomNo);

}